import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import sma.interf.BindUtil;
import sma.interf.ICalculateIss;
import sma.interf.SyncCalculateImpl;

/**
 * Caculator server
 * 
 * @author smarsoll
 * 
 */
public class CalculateIssSrv {

	/** Logger for this class. */
	protected final static Logger LOGGER = LoggerFactory.getLogger(CalculateIssSrv.class);

	// public CalculateIssSrv() {
	// }

	public static void main(String args[]) {
		String strUrl = BindUtil.getBindName(2);
		try {
			ICalculateIss issCalculator = new SyncCalculateImpl();
			// ICalculateIss stub = (ICalculateIss) UnicastRemoteObject.exportObject(issCalculator, 0);
			// Naming.rebind(strUrl, issCalculator);
			Registry registry = LocateRegistry.getRegistry();
			String[] alreadybound = registry.list();
			trace(alreadybound);

			strUrl = BindUtil.getFreeBindName(alreadybound);

			registry.bind(strUrl, issCalculator);
			// registry.bind("rmi://marsolle-2/coucou", issCalculator);
			LOGGER.info("done bind on " + strUrl);
			while (true) {
				try {
					Thread.sleep(60000);
				} catch (InterruptedException e) {
					LOGGER.warn("interupted");
				}

			}
		} catch (Exception e) {
			LOGGER.error("", e);
		}
		LOGGER.info("end");
	}

	private static void trace(String[] alreadybound) {
		LOGGER.info("alreadybound {}", alreadybound.length);
		for (int i = 0; i < alreadybound.length; i++) {
			LOGGER.info(alreadybound[i]);
		}

	}

}
